package com.gilight.base.controller;

import com.gilight.base.admin.advert.AdminAdvertService;
import com.gilight.base.admin.user.AdminUserService;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.base.exception.ServiceException;
import com.gilight.base.model.AdvertDO;
import com.gilight.base.model.Page;
import com.gilight.base.model.uni.UserDO;
import com.gilight.base.service.SysUserZzjgService;
import com.gilight.base.util.MapUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * @author liken
 * 商品管理控制类
 */
@RestController
@RequestMapping("/web/admin-advert")
public class AdminAdvertController {
    @Autowired
    private AdminAdvertService adminAdvertService;
    @Autowired
    private SysUserZzjgService sysUserZzjgService;
    /**
     *
     * @param type 广告类型
     * @param unionType 关联类型
     * @param title  广告标题
     * @param unionValue 关联值
     * @param imgUrl 广告图片地址
     * @param status 广告状态
     * @param color   广告图片颜色  valueDef = "rgb(255,255,255)"
     * @param adminId 广告图片颜色
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "创建")
    @RequestMapping(value = "/create")
    public ResultEntity create(
            Integer type,
            Integer unionType,
            String title,
            String unionValue,
            String imgUrl,
            Integer status,
            String color,
            Long adminId,
            Integer orderC,
            Long brandId,
            Long zzjgId) throws ServiceException{
        if(zzjgId == null){
            List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
            zzjgId = 1L;
            if(zzjgsUser!=null&&zzjgsUser.size()>0) {
                zzjgId =  MapUtils.getLong(zzjgsUser.get(0), "zzjgId");
            }
        }

        String cr = adminAdvertService.create(type,unionType,title,unionValue,imgUrl,status,color,adminId,zzjgId,orderC,brandId);
        ResultEntity resultEntity = new ResultEntity(cr);
        return resultEntity;

    }

    /**
     * 删除
     * @param type 广告类型
     * @param adId 广告ID
     * @param adminId 管理员ID
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete")
    public ResultEntity delete(
            Integer type,
            Long adId,
            Long adminId) throws ServiceException{
        String cr = adminAdvertService.delete(type,adId,adminId);
        ResultEntity resultEntity = new ResultEntity(cr);
        return resultEntity;
    }

    /**
     *
     * @param adId 广告ID
     * @param type 广告类型
     * @param unionType 关联类型
     * @param title 广告标题
     * @param unionValue 关联值
     * @param imgUrl 广告图片地址
     * @param status 广告状态
     * @param color 广告图片颜色
     * @param adminId
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "更新")
    @RequestMapping(value = "/update")
    public ResultEntity edit(
            Long adId,
            Integer type,
            Integer unionType,
            String title,
            String unionValue,
            String imgUrl,
            Integer status,
            String color,
            Long adminId,
            Long brandId,
            Integer orderC) throws ServiceException{
        String cr = adminAdvertService.edit(adId,type,unionType,title,unionValue,imgUrl,status,color,adminId,orderC,brandId);
        ResultEntity resultEntity = new ResultEntity(cr);
        return resultEntity;
    }

    /**
     *
     * @param adminId 管理员ID
     * @param type 广告类型
     * @param page
     * @param limit
     * @param status 广告状态
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "所有")
    @RequestMapping(value = "/list")
    public ResultEntity list(
            Long adminId,
            Integer type,
            Integer page,
            Integer limit,
            Integer status,
            Long zzjgId) throws ServiceException{
        List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
        if(zzjgId == null){
             zzjgId = 1L;
            if(zzjgsUser!=null&&zzjgsUser.size()>0) {
                zzjgId =  MapUtils.getLong(zzjgsUser.get(0), "zzjgId");
            }
        }

        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminAdvertService.list(adminId,type,page,limit,status,zzjgId));

        return resultEntity;
    }
}
